<template>
	<view>
		<view class="top">
			<view class="">
				<view @click="goBack">
					<image class="back" src="/static/images/白色左箭头.png" mode=""></image>
				</view>
				<view class="title">
					订单详情
				</view>
			</view>
			<view style="font-size: 13px; margin-top: 10px; margin-left: 30px; color: white;">
				支付成功，请您前往指定门店消费
			</view>
			<view style="display: flex;margin-left: 30px;margin-top: 15px;">
				<image style=" margin-top: 3px; width: 20px; height: 20px;" src="../../../static/images/商家.png" mode="">
				</image>
				<view style="color: white; margin-left: 10px;">
					<view style="font-size: 15px;">
						{{orderInfo.merchantName || '加载中...'}}
					</view>
					<view style="font-size: 13px;margin-top: 5px;">
						{{orderInfo.merchantAddress || '加载中...'}}
					</view>
				</view>
				<image style="margin-top: 30px; right: 20px; position: fixed; width: 16px;height: 16px;"
					src="../../../static/images/plane.png" mode=""></image>
			</view>
		</view>
		<uni-card>
			<view style="font-weight: bold; margin-top: 10px; padding-bottom: 10px; border-bottom: 1px solid gainsboro;">
				订单详情
			</view>
			<view style="display: flex;margin-top: 10px;">
				<view style="font-size: 13px;font-weight: bold;">
					车辆精洗
				</view>
				<view style=" position: fixed; right: 30px; font-size: 13px;font-weight: bold;">
					￥{{orderInfo.servicePrice || '0.00'}}
				</view>
			</view>
			<view style="display: flex;margin-top: 10px;padding-bottom: 10px; border-bottom: 1px solid gainsboro;">
				<view style="font-size: 13px;font-weight: bold;">
					车辆附加费用
				</view>
				<view style=" position: fixed; right: 30px; font-size: 13px;font-weight: bold;">
					￥0.00
				</view>
			</view>
			<view style="display: flex;margin-top: 10px;">
				<view style="font-size: 13px;font-weight: bold;">
					原价
				</view>
				<view style=" position: fixed; right: 30px; font-size: 13px;font-weight: bold;">
					￥{{orderInfo.originalPrice || '0.00'}}
				</view>
			</view>
			<view style="display: flex;margin-top: 10px;">
				<view style="font-size: 13px;font-weight: bold;">
					店家优惠
				</view>
				<view style=" color: #FF4407; position: fixed; right: 30px; font-size: 12px;font-weight: bold;">
					-￥{{orderInfo.merchantDiscount || '0.00'}}
				</view>
			</view>
			<view style="display: flex;margin-top: 10px;padding-bottom: 10px; border-bottom: 1px solid gainsboro;">
				<view style="font-size: 13px;font-weight: bold;">
					优惠券
				</view>
				<view style=" color: #FF4407; position: fixed; right: 30px; font-size: 12px;font-weight: bold;">
					-￥{{orderInfo.couponDiscount || '0.00'}}
				</view>
			</view>
			<view style="display: flex;margin-top: 20px;margin-bottom: 10px;">
				<view style="font-size: 15px;font-weight: bold;">
					支付金额
				</view>
				<view style=" color: #84DEA0; position: fixed; right: 30px; font-size: 20px ;font-weight: bold;">
					￥{{orderInfo.orderPrice || '0.00'}}
				</view>
			</view>
		</uni-card>
		<uni-card>
			<view style="font-weight: bold; margin-top: 10px; padding-bottom: 10px; border-bottom: 1px solid gainsboro;">
				订单信息
			</view>
			<view style="display: flex;margin-top: 10px;">
				<view style="font-size: 13px;font-weight: bold;">
					订单号：
				</view>
				<view style=" position: fixed; right: 30px; font-size: 13px;font-weight: bold;">
					{{orderInfo.orderNum || '加载中...'}}
				</view>
			</view>
			<view style="display: flex;margin-top: 10px;">
				<view style="font-size: 13px;font-weight: bold;">
					下单时间：
				</view>
				<view style=" position: fixed; right: 30px; font-size: 13px;font-weight: bold;">
					{{orderInfo.orderTime || '加载中...'}}
				</view>
			</view>
			<view style="display: flex;margin-top: 10px;">
				<view style="font-size: 13px;font-weight: bold;">
					核销码：
				</view>
				<view style=" position: fixed; right: 30px; font-size: 13px;font-weight: bold;">
					{{orderInfo.verificationCode || '加载中...'}}
				</view>
			</view>
		</uni-card>
	</view>
</template>

<script>
import request from '../../../uitls/request';

export default {
	data() {
		return {
			orderNum: '',
			orderInfo: {}
		}
	},
	onLoad(options) {
		console.log('订单详情页面接收到的参数:', options);
		if (options.orderNum) {
			this.orderNum = options.orderNum;
			// 从本地存储获取订单详情
			const orderDetails = uni.getStorageSync('currentOrderDetails');
			if (orderDetails) {
				console.log('从本地存储获取的订单详情:', orderDetails);
				this.orderInfo = orderDetails;
				// 清除本地存储
				uni.removeStorageSync('currentOrderDetails');
			} else {
				// 如果没有本地存储的订单详情，从服务器获取
				this.fetchOrderDetails();
			}
		}
	},
	methods: {
		goBack() {
			uni.navigateBack({
				delta: 1
			});
		},
		async fetchOrderDetails() {
			try {
				const res = await new Promise((resolve, reject) => {
					request({
						url: '/xsc/orders/' + this.orderNum,
						method: 'GET',
						success: resolve,
						fail: reject
					});
				});

				console.log('获取到的订单详情:', res);
				if (res.data.code === 200) {
					this.orderInfo = res.data.data;
					// 获取商家信息
					if (this.orderInfo.merchantId) {
						this.fetchMerchantInfo(this.orderInfo.merchantId);
					}
				} else {
					uni.showToast({
						title: res.data.msg || '获取订单详情失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('获取订单详情失败:', error);
				uni.showToast({
					title: '获取订单详情失败',
					icon: 'none'
				});
			}
		},
		async fetchMerchantInfo(merchantId) {
			try {
				const res = await new Promise((resolve, reject) => {
					request({
						url: '/xsc/merchant/' + merchantId,
						method: 'GET',
						success: resolve,
						fail: reject
					});
				});

				if (res.data.code === 200) {
					// 更新商家信息
					this.orderInfo.merchantName = res.data.data.name;
					this.orderInfo.merchantAddress = res.data.data.address;
				}
			} catch (error) {
				console.error('获取商家信息失败:', error);
			}
		}
	}
}
</script>

<style scoped>
	.top {
		width: 100%;
		padding-bottom: 60px;
		background: url('../../../static/images/组 8.png');
		background-repeat: no-repeat;
		background-size: cover;
	}

	.top .back {
		position: absolute;
		margin-left: 20px;
		margin-top: 30px;
		width: 15px;
		height: 15px;
		z-index: 2;
	}

	.top .title {
		text-align: center;
		font-size: 18px;
		color: #fff;
		line-height: 44px;
		/* 根据实际需要调整 */
		padding-top: 20px;
	}
</style>